﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace ResUma
{
    public partial class CarteMuncaFrm : FrmWorkspace
    {
        public CarteMuncaFrm(ModuleClass mc):base(mc)
        {
            InitializeComponent();
            CMGrid.m_AutoCompletionColumns.Add(iDAngajatorColumn);
            //cartiMuncaDataSet.CarteMuncaIstoric.DeLaDataColumn.DefaultValue = DateTime.Now;
            ///cartiMuncaDataSet.CarteMuncaIstoric.PanaLaDataColumn.DefaultValue = DateTime.Now;
            ///
            dataPrAngaDTP.Format = DateTimePickerFormat.Custom;
            dataPrAngaDTP.CustomFormat = "dd.MM.yyyy";
            dataUnitDTP.Format = DateTimePickerFormat.Custom;
            dataUnitDTP.CustomFormat = "dd.MM.yyyy";
            NouDTP.Format = DateTimePickerFormat.Custom;
            NouDTP.CustomFormat = "dd.MM.yyyy";
            this.persSalDataSet.EnforceConstraints = false;
        }

        private void CarteMuncaFrm_Load(object sender, EventArgs e)
        {
            gridRezumat.Remove(PersoaneDGV);
            SqlConnection ESRPCon = DeschideConexiune(workConnString);
            
            SqlConnection PersalCon = DeschideConexiune("PerssalConnectionString");
            this.dsc_ViewCarteMuncaIstoricTableAdapter.Connection = PersalCon;
            this.dsc_ViewCarteMuncaIstoricTableAdapter.Fill(this.persSalDataSet.dsc_ViewCarteMuncaIstoric);

            this.pLocMuncaTableAdapter.Connection = PersalCon;
            this.pLocMuncaTableAdapter.Fill(this.persSalDataSet.pLocMunca);
            InchideConexiune(PersalCon);

            this.persoaneTableAdapter.Connection = ESRPCon;
            this.persoaneTableAdapter.Fill(this.cartiMuncaDataSet.Persoane);
            this.carteMuncaIstoricTableAdapter.Connection = ESRPCon;
            this.carteMuncaIstoricTableAdapter.Fill(this.cartiMuncaDataSet.CarteMuncaIstoric);
            this.carteMuncaAngajatorTableAdapter.Connection = ESRPCon;
            this.carteMuncaAngajatorTableAdapter.Fill(this.cartiMuncaDataSet.CarteMuncaAngajator);

            InchideConexiune(ESRPCon);

            
            AddBindingSource(fKCarteMuncaIstoricPersoaneBindingSource);
            AddTableAdaptor(carteMuncaIstoricTableAdapter.Adapter);

            //AddBindingSource(pLocMuncadscViewCarteMuncaIstoricBindingSource);
            //AddTableAdaptor(dsc_ViewCarteMuncaIstoricTableAdapter.Adapter);
            
            

        }

        override public void Salvare(object sender, EventArgs args)
        {
            
            this.SuspendLayout();
            CMGrid.Enabled = false;

            base.Salvare(this, args);
            CMGrid.Enabled = true;
            ResumeLayout();
        }


        private void pLocMuncadscViewCarteMuncaIstoricBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            if(ViewCarteMuncaIstoricBindingSource.Current==null)
                return;
            PersSalDataSet.dsc_ViewCarteMuncaIstoricRow  rand =  ((DataRowView)ViewCarteMuncaIstoricBindingSource.Current).Row 
                as PersSalDataSet.dsc_ViewCarteMuncaIstoricRow;
            persoaneBindingSource.Filter = "Marca='" + rand.Marca.ToString()+"'";
            NouDTP.Value = DateTime.Now;
            
        }


        private void CMGrid_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.ColumnIndex == iDAngajatorColumn.Index)
            { 
                String denCauta = e.FormattedValue.ToString().Trim().ToUpper();
                if (denCauta == String.Empty)
                    return;
                int pozGasita = AngajatorBindingSource.Find("Denumire", denCauta);
                if (pozGasita < 0)
                {
                    CartiMuncaDataSet.CarteMuncaAngajatorRow ar = ((DataRowView)AngajatorBindingSource.AddNew()).Row as CartiMuncaDataSet.CarteMuncaAngajatorRow;
                    ar.Denumire = denCauta;
                    AngajatorBindingSource.EndEdit();
                    SqlConnection ESRPCon = DeschideConexiune(workConnString);
                    this.carteMuncaAngajatorTableAdapter.Connection = ESRPCon;
                    carteMuncaAngajatorTableAdapter.Update(this.cartiMuncaDataSet.CarteMuncaAngajator);
                    this.carteMuncaAngajatorTableAdapter.Fill(this.cartiMuncaDataSet.CarteMuncaAngajator);
                    InchideConexiune(ESRPCon);

                    AngajatorBindingSource.Position = AngajatorBindingSource.Find("Denumire", denCauta);
                    ar = ((DataRowView)AngajatorBindingSource.Current).Row as CartiMuncaDataSet.CarteMuncaAngajatorRow;
                    CartiMuncaDataSet.CarteMuncaIstoricRow isrow = ((DataRowView)fKCarteMuncaIstoricPersoaneBindingSource.Current).Row as CartiMuncaDataSet.CarteMuncaIstoricRow;
                    isrow.IDAngajator = ar.IDAngajator;
                    CMGrid[e.ColumnIndex, e.RowIndex].Value = ar.IDAngajator;
                    if (CMGrid.CurrentCell.IsInEditMode)
                        CMGrid.EditingControl.Text = denCauta;
                    

                }
            }
        }

        private void dataPrAngaDTP_Validated(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("",DeschideConexiune("PerssalConnectionString"));
            if(ViewCarteMuncaIstoricBindingSource.Current==null)
                return;
            PersSalDataSet.dsc_ViewCarteMuncaIstoricRow drow = ((DataRowView)ViewCarteMuncaIstoricBindingSource.Current).Row as PersSalDataSet.dsc_ViewCarteMuncaIstoricRow;
            cmd.CommandText = "UPDATE pPerson SET DataPrAnga='" + dataPrAngaDTP.Value.Year.ToString() + "-" + dataPrAngaDTP.Value.Month.ToString() + "-" + dataPrAngaDTP.Value.Day.ToString() + "' WHERE " +
                "Marca = " + drow.Marca.ToString() + " AND Cod_Unit=" + drow.Cod_Unit.ToString() ;
            cmd.ExecuteNonQuery();
            InchideConexiune(cmd.Connection);
        }

        

        
    }
}
